﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AMS.DataAccess;
using AMS.ORM.Entity;
using AMS.DataAccess.Interfaces;
using AMS.DataAccess.BO;
using AMS.DataAccess.BO.Enums;

public partial class Customer_ShowCustomer : BaseWorkPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (ParseIntParameter("id", out itemId))
            {
                DoDataBind();
            }
            else
            {
                GlobalTransfer.RedirectToErrorPage("无效的ID参数.");
            }
        }
    }

    private int itemId = 0;
    void DoDataBind()
    {
        ICustomerManager customerMgr = ManagerFactory.CreateCustomerManager();
        IContactorManager contactorMgr = ManagerFactory.CreateContactorManager();
        CustomerInfo customer = customerMgr.GetCustomer(itemId);
        customer.Contactors = contactorMgr.GetCustomerContactors(CallContext, customer.AID);

        if (customer != null)
        {
            ShowCustomer(customer);
        }
        else
        {
            MsgError("未找到记录，可能已被删除。");
        }
    }

    private void ShowCustomer(CustomerInfo customer)
    {
        //TODO: show to the ui
        lblCustomerName.Text = customer.Name;
        lblName.Text = customer.Name;
        lblAddress.Text = customer.Address;
        lblCity.Text = customer.ProvinceName;
        lblCitySub.Text = customer.CityName;
        lblCode.Text = customer.IDCode;
        lblCreateDate.Text = customer.CreateDate.ToShortDateString();
        lblCredit.Text = customer.CreditLevel.ToString();
        lblCustomerType.Text = EnumConverter.CustomerTypeToString(customer.CustomerType);
        lblBusinessType.Text = EnumConverter.CustomerBusinessTypeToString(customer.BusinessType);
        lblHomepage.Text = customer.HomePage;
        lblImportance.Text = "";
        lblPosition.Text = customer.RepresentPosition;
        lblRepresenter.Text = customer.Represent;
        lblNote.Text = customer.Note;
        lblSpecialNeeds.Text = customer.SpecialNeeds;

        if (customer.Contactors != null && customer.Contactors.Count > 0)
        {
            ContactorInfo contactor = customer.Contactors[0];
            lblContactor.Text = contactor.Name;
            lblFax.Text = contactor.Fax;
            lblMail.Text = contactor.Email;
            lblTel.Text = contactor.Tel;
            lblZipcode.Text = contactor.Zipcode;
        }

        if (customer.CreditLevel == CustomerCreditLevel.Vip)
        {
            lblCreditLevel.Text = "(VIP)";
        }
    }
}
